package com.sczmgk.track.activity;

import android.os.Bundle;
import android.os.Message;

import com.sczmgk.track.base.BaseActivity;
import com.sczmgk.track.interf.OnRefreshListener;
import com.sczmgk.track.utils.MyLog;
import com.sczmgk.track.utils.MyToast;
import com.sczmgk.track.view.refreshview.RefreshListView;

import java.util.Map;

public abstract class BaseRefreshActivity  extends BaseActivity {


    /**
     * 默认第一次加载
     */
    protected final int LOAD_NORMAL = 0;
//    /**
//     * 下拉刷新状态
//     */
//    protected final int PULL_REFRESH = 1;
    /**
     * 上拉加载更多
     */
    protected final int LOAD_MORE = 2;
    protected final int REFRESH_LIST = 1;
    protected final int HIDE_FOOTER_HEADER = 102;
    protected RefreshListView mListView;
    protected String keyword= "";
    protected int totalpages;
    protected int nowpage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


    public abstract void loadData(int page,String keyword,int loadStatus);

    public void setListeners(){
        mListView.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onPullDownRefresh() {
                MyLog.i(TAG, "下拉刷新----");
                pullDownRefresh();
            }

            @Override
            public void onLoadingMore() {
                MyLog.d(TAG,"加载更多");
                loadMore();
            }
        });
    }




    protected void pullDownRefresh(){
        loadData(1, keyword, LOAD_NORMAL);
    }

    protected void loadMore(){

        if (totalpages > nowpage) {
            loadData(nowpage + 1, keyword, LOAD_MORE);

        } else {
            Message msg = Message.obtain();
            msg.what = HIDE_FOOTER_HEADER;
            mHandler.sendMessage(msg);
            MyToast.show(this, "没有更多数据！");
        }
    }

    /**
     * 发消息更新list
     */
    protected void refreshList() {
        Message msg = Message.obtain();
        msg.what = REFRESH_LIST;
        mHandler.sendMessage(msg);
    }

    /**
     * 子类实现取得说请求maps
     *
     * @param page
     * @param keyword
     * @return
     */
    public abstract Map<String, Object> getRequestData(int page, String keyword);

    /**
     * 子类实现取得urL
     *
     * @return
     */
    public abstract String getRquestUrl();

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case HIDE_FOOTER_HEADER:

                if (mListView != null) {
                    mListView.hideFooterView();
                    mListView.hideHeaderView();
                }
                break;
        }
    }
}
